package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.company.api.SysCompanyService;
import com.nidu.demo.company.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 公司管理 Controller
 */
@Tag(name = "管理后台 - 公司")
@RestController
@RequestMapping("/system/company")
@Validated
@RequiredArgsConstructor
public class SysCompanyController {

    private final SysCompanyService service;

    @Operation(summary = "创建公司", description = "创建新的公司")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:company:create')")
    public SingleResponse<CompanyCO> addCompany(@RequestBody CompanyAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新公司", description = "更新公司的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:company:update')")
    public SingleResponse<CompanyCO> updateCompany(@PathVariable Long id, @RequestBody CompanyUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除公司", description = "根据 ID 删除公司")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:company:delete')")
    public Response deleteCompany(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除公司", description = "根据 ID 列表删除多个公司")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:company:delete')")
    public Response deleteCompanies(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取公司详情", description = "根据 ID 获取公司详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:company:query')")
    public SingleResponse<CompanyCO> getCompanyById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询公司列表", description = "获取所有公司信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:company:query')")
    public MultiResponse<CompanyCO> listCompanies(CompanyListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "分页查询公司", description = "分页获取公司信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:company:query')")
    public PageResponse<CompanyCO> pageCompanies(CompanyPageQry qry) {
        return service.page(qry);
    }

    @Operation(summary = "更新公司状态", description = "更新公司的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:company:update')")
    public Response updateStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return service.updateStatus(id, status);
    }
}
